package example.fileupload;

import grpc.htsoft.fileupload.FileServiceGrpc;
import grpc.htsoft.fileupload.FileUploadRequest;
import grpc.htsoft.fileupload.FileUploadResponse;
import grpc.htsoft.fileupload2.FileUploadServiceGrpc;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.IOException;

public class FileServer2 {
    public static void main(String[] agrc) throws IOException, InterruptedException {
        System.out.println("server2");
        Server server = ServerBuilder
                .forPort(50092)
                .addService(new FileUploadService2())
                .build();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("gRPC server is shutting down!");
            server.shutdown();
        }));
        System.out.println("Starting server...");
        server.start();
        System.out.println("Server started!");
        server.awaitTermination();

//        server.blockUntilShutdown();
    }
    static class FileUploadService2 extends FileServiceGrpc.FileServiceImplBase {

        public StreamObserver<FileUploadRequest> upload(StreamObserver<FileUploadResponse> responseObserver) {
            return new StreamObserver<FileUploadRequest>() {
                @Override
                public void onNext(FileUploadRequest fileUploadRequest) {

                    System.out.println("onnext");
                }

                @Override
                public void onError(Throwable throwable) {
                    System.out.println("onError");
                }

                @Override
                public void onCompleted() {
                    System.out.println("onNext onCompleted");
                }
            };
        }

    }
}
